package at.com.vn.chess.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import at.com.vn.chess.bean.RoomBean;
import at.com.vn.chess.dao.RoomDao;
import at.com.vn.chess.dao.UserDao;
import at.com.vn.chess.entity.User;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	RoomBean lstRoom = new RoomBean();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegisterServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String message = "";
		String destination = "";	
		try {

			HttpSession session = request.getSession(true);

			User userForm = new User();
			userForm.setUser_id(String.valueOf(request.getParameter("name")));
			userForm.setUser_password(String.valueOf(request.getParameter("pass")));
			userForm.setUser_email(String.valueOf(request.getParameter("email")));
			String checkAvail = request.getParameter("checkAvai");

			// Save input value when err
			session.setAttribute("inputUser", userForm);

			UserDao userDao = new UserDao();
			userDao.beginTrans();

			// Check avail click
			if (checkAvail.equals("1")) {
				if (userDao.findUser(userForm.getUser_id()) != null) {
					message = "user name available, pls choose other";
				} else {
					message = "user name OK";
				}
				// Set value for session
				session.setAttribute("availMessage", message);
				destination = "/pages/register.jsp"; // error page
			}
			// submit click
			if (checkAvail.equals("0")) {
				if (userDao.findUser(userForm.getUser_id()) != null) {
					message = "user name available, pls choose other";
					// Set value for session
					session.setAttribute("availMessage", message);
					destination = "/pages/register.jsp"; // error page
				} else {
					// Register new user
					User userReg = userDao.registUser(userForm);
					userDao.commitTrans();

					if (userReg != null) {
						// Set session for user
						session.setAttribute("currentSessionUser", userForm);

						// Get data from database
						RoomDao roomDao = new RoomDao();
						roomDao.beginTrans();
						lstRoom.setLstRoom(roomDao.findAllRoom());
						roomDao.closeTrans();

						// set bean for load roomList
						request.setAttribute("lstRoomBean", lstRoom);

						destination = "/pages/roomList.jsp"; // logged-in page

					}
				}
			}

			userDao.closeTrans();

			RequestDispatcher rd = getServletContext().getRequestDispatcher(
					destination);
			rd.forward(request, response);
		}

		catch (Throwable theException) {
			System.out.println(theException);
		}
	}
}
